package com.junjiao.util.java;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zsy
 */

public final class Ubbcode {

	private static final String imagePath = "images/"; // 定义图片路径
	private static final String strIcons = "1";
	private static final String picUrl = "edit/images/";
	private static final String imgName = "edit/images/face/";

	private static final String notePath = "http://www.richmap.com.cn/richmap4/api/tools/";

	private static String ubbItem(String strContent, String re,
			String replayStr, Pattern pattern, Matcher matcher,
			boolean IgnoreCase) {
		pattern = Pattern.compile(re);
		matcher = pattern.matcher(strContent);
		strContent = matcher.replaceAll(replayStr);
		if (IgnoreCase) {
			pattern = Pattern.compile(re.toUpperCase());
			matcher = pattern.matcher(strContent);
			strContent = matcher.replaceAll(replayStr);
		}
		return strContent;
	}

	private static String quote(String strContent, Pattern pattern,
			Matcher matcher, boolean IgnoreCase) {
		String re = "(\\[quote\\])(.*?)(\\[\\/quote\\])";
		int start = strContent.lastIndexOf("[quote]");
		int end = strContent.indexOf("[/quote]");
		String subStr = null;
		String newStr = null;
		while (start != -1 && end != -1) {
			subStr = strContent.substring(start, end + 8);
			pattern = Pattern.compile(re);
			matcher = pattern.matcher(subStr);
			newStr = matcher
					.replaceAll("<TABLE style=\"BORDER-RIGHT: #cccccc 1px dotted; "
							+ "TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: "
							+ "#cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted\" cellSpacing=0 "
							+ "cellPadding=6 width=95% align=center border=0><TBODY><TR><TD "
							+ "style=\"WORD-WRAP: break-word;font-size:12px;\" bgColor=#f3f3f3><FONT "
							+ "style=\"FONT-WEIGHT: bold; COLOR: #990000; font-size:12px;\">***以下是引"
							+ "用***</FONT><HR width=95% style=\"border:1px dotted  #cccccc \"><center>"
							+ "$2</center></TD></TR></TBODY></TABLE>");
			strContent = strContent.replace(subStr, newStr);
			start = strContent.lastIndexOf("[quote]");
			end = strContent.indexOf("[/quote]");
		}

		if (IgnoreCase) {
			re = "(\\[QUOTE\\])(.*?)(\\[\\/QUOTE\\])";
			start = strContent.lastIndexOf("[QUOTE]");
			end = strContent.indexOf("[/QUOTE]");
			while (start != -1 && end != -1) {
				subStr = strContent.substring(start, end + 8);
				pattern = Pattern.compile(re);
				matcher = pattern.matcher(subStr);
				newStr = matcher
						.replaceAll("<TABLE style=\"BORDER-RIGHT: #cccccc 1px dotted; "
								+ "TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: "
								+ "#cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted\" cellSpacing=0 "
								+ "cellPadding=6 width=95% align=center border=0><TBODY><TR><TD "
								+ "style=\"WORD-WRAP: break-word;font-size:12px;\" bgColor=#f3f3f3><FONT "
								+ "style=\"FONT-WEIGHT: bold; COLOR: #990000; font-size:12px;\">***以下是引"
								+ "用***</FONT><HR width=95% style=\"border:1px dotted  #cccccc \"><center>"
								+ "$2</center></TD></TR></TBODY></TABLE>");
				strContent = strContent.replace(subStr, newStr);
				start = strContent.lastIndexOf("[QUOTE]");
				end = strContent.indexOf("[/QUOTE]");
			}
		}
		return strContent;
	}

	/**
	 * @description 转换代码，使之支持ubbcode.
	 * @param strContent
	 *            转换前的代码.
	 * @return result 转换后的内容.
	 */
	public static String ubbCode(String strContent) {
		strContent = dvHTMLEncode(strContent);
		Pattern pattern = null;
		Matcher matcher = null;

		// 随意贴
		strContent = ubbItem(
				strContent,
				"\\[richmapfrm=(.[^\\[]*)\\](.+?)\\[\\/richmapfrm\\]",
				"<iframe src="
						+ notePath
						+ "singleNote.jsp?w=600&h=480"
						+ "&noteid=$1 frameborder=0 marginheight=0 marginwidth=0 scrolling=no height=400 "
						+ "width=600 title=睿图随意贴,贴出你的故事,贴出你的心情 target=_blank><img src="
						+ picUrl
						+ "button/richiframe.gif border=0> $2</iframe>",
				pattern, matcher, true);

		strContent = ubbItem(strContent,
				"\\[richmap=(.[^\\[]*)\\](.+?)\\[\\/richmap\\]", "<a href="
						+ notePath + "myNote.jsp?&noteid=$1 "
						+ "title=睿图随意贴,贴出你的故事,贴出你的心情 target=_blank><img src="
						+ picUrl + "button/richwindow.gif border=0>$2</a>",
				pattern, matcher, true);

		// 转换IMG
		strContent = ubbItem(
				strContent,
				"\\[img\\](.+?)\\[\\/img\\]",
				"<a href=\"$1\" target=\"_blank\">"
						+ "<IMG SRC=\"$1\" border=\"0\" alt=\"按此在新窗口浏览图片\" "
						+ "onload=\"javascript:if(this.width>600)this.width=600\" "
						+ "galleryImg=\"no\"></a>", pattern, matcher, true);

		// 转换附件[FILE=路径,图片]名称[/FILE]
		strContent = ubbItem(strContent,
				"(\\[file=(.[^\\[]*),(.[^\\[]*)\\])(.*?)(\\[\\/file\\])",
				"<br><IMG SRC=$3 border=0><br> <a href=$2>$4</a>", pattern,
				matcher, true);

		// DIR
		strContent = ubbItem(
				strContent,
				"\\[dir=*([0-9]*),*([0-9]*)\\](.*?)\\[\\/dir]",
				"<object classid=clsid:"
						+ "166B1BCA-3F9C-11CF-8075-444553540000 codebase="
						+ "http://download.macromedia.com/pub/shockwave/cabs/director/"
						+ "sw.cab#version=7,0,2,0 width=$1 height=$2><param name=src "
						+ "value=$3><embed src=$3 pluginspage=http://www.macromedia.com/"
						+ "shockwave/download/ width=$1 height=$2></embed></object>",
				pattern, matcher, true);

		// QT
		strContent = ubbItem(
				strContent,
				"\\[qt=*([0-9]*),*([0-9]*)\\](.*?)\\[\\/qt]",
				"<embed src=$3 width=$1 height=$2 "
						+ "autoplay=true loop=false controller=true playeveryframe=false "
						+ "cache=false scale=TOFIT bgcolor=#000000 kioskmode=false "
						+ "targetcache=false pluginspage=http://www.apple.com/quicktime/>",
				pattern, matcher, true);

		// MP
		strContent = ubbItem(
				strContent,
				"\\[mp=*([0-9]*),*([0-9]*)\\](.*?)\\[\\/mp]",
				"<object align=middle classid=CLSID:"
						+ "22d6f312-b0f6-11d0-94ab-0080c74c7e95 class=OBJECT id=MediaPlayer "
						+ "width=$1 height=$2 ><param name=ShowStatusBar value=-1><param "
						+ "name=Filename value=$3><embed type=application/x-oleobject "
						+ "codebase=http://activex.microsoft.com/activex/controls/mplayer/"
						+ "en/nsmp2inf.cab#Version=5,1,52,701 "
						+ "flename=mp src=$3  width=$1 height=$2></embed></object>",
				pattern, matcher, true);

		// RM
		strContent = ubbItem(
				strContent,
				"\\[rm=*([0-9]*),*([0-9]*)\\](.*?)\\[\\/rm]",
				"<OBJECT classid=clsid:"
						+ "CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX "
						+ "width=$1 height=$2><PARAM NAME=SRC VALUE=$3><PARAM NAME=CONSOLE "
						+ "VALUE=Clip1><PARAM NAME=CONTROLS VALUE=imagewindow><PARAM "
						+ "NAME=AUTOSTART VALUE=true></OBJECT><br><OBJECT classid=CLSID:"
						+ "CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=32 id=video2 "
						+ "width=$1><PARAM NAME=SRC VALUE=$3><PARAM NAME=AUTOSTART VALUE=-1>"
						+ "<PARAM NAME=CONTROLS VALUE=controlpanel><PARAM NAME=CONSOLE "
						+ "VALUE=Clip1></OBJECT>", pattern, matcher, true);

		// FLASH
		strContent = ubbItem(
				strContent,
				"(\\[flash\\])(.*?)(\\[\\/flash\\])",
				"<OBJECT codeBase=http:"
						+ "//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"
						+ "#version=6,0,29,0 classid=clsid:"
						+ "D27CDB6E-AE6D-11cf-96B8-444553540000 width=500 height=400><PARAM "
						+ "NAME=movie VALUE=$2><PARAM NAME=quality VALUE=high><embed src=$2 "
						+ "quality=high pluginspage='http://www.macromedia.com/go/"
						+ "getflashplayer' type='application/x-shockwave-flash' "
						+ "width=500 height=400>$2</embed></OBJECT>", pattern,
				matcher, true);
		strContent = ubbItem(
				strContent,
				"(\\[flash=*([0-9]*),*([0-9]*)\\])(.*?)(\\[\\/flash\\])",
				"<a href=$4 TARGET=_blank><IMG SRC="
						+ picUrl
						+ "button/falsh.gif border=0 "
						+ "alt=点击开新窗口欣赏该FLASH动画!> [全屏欣赏]</a><br><br><OBJECT "
						+ "codeBase=http://download.macromedia.com/pub/shockwave/cabs/"
						+ "flash/swflash.cab#version=6,0,29,0 classid=clsid:"
						+ "D27CDB6E-AE6D-11cf-96B8-444553540000 width=$2 height=$3><PARAM "
						+ "NAME=movie VALUE=$4><PARAM NAME=quality VALUE=high><param "
						+ "name=menu value=false><embed src=$4 quality=high menu=false "
						+ "pluginspage='http://www.macromedia.com/go/getflashplayer' "
						+ "type='application/x-shockwave-flash' width=$2 height=$3>$4"
						+ "</embed></OBJECT>", pattern, matcher, true);

		// 转换UPLOAD
		strContent = ubbItem(
				strContent,
				"(\\[upload=gif\\])(.*?)(\\[\\/upload\\])",
				"<br><IMG SRC="
						+ imagePath
						+ "gif.gif border=0>此主题相关图片如下：<br><A HREF=$2 TARGET=_blank>"
						+ "<IMG SRC=$2 border=0 alt=按此在新窗口浏览图片 onload=\"javascript:"
						+ "if(this.width>screen.width-333)this.width=screen.width-333\"></A>",
				pattern, matcher, true);
		strContent = ubbItem(
				strContent,
				"(\\[upload=jpg\\])(.*?)(\\[\\/upload\\])",
				"<br><IMG SRC="
						+ imagePath
						+ "jpg.gif border=0>此主题相关图片如下：<br><A HREF=$2 TARGET=_blank>"
						+ "<IMG SRC=$2 border=0 alt=按此在新窗口浏览图片 onload=\"javascript:"
						+ "if(this.width>screen.width-333)this.width=screen.width-333\"></A>",
				pattern, matcher, true);
		strContent = ubbItem(
				strContent,
				"(\\[upload=bmp\\])(.*?)(\\[\\/upload\\])",
				"<br><IMG SRC="
						+ imagePath
						+ "jpg.gif border=0>此主题相关图片如下：<br><A HREF=$2 TARGET=_blank>"
						+ "<IMG SRC=$2 border=0 alt=按此在新窗口浏览图片 onload=\"javascript:"
						+ "if(this.width>screen.width-333)this.width=screen.width-333\"></A>",
				pattern, matcher, true);
		strContent = ubbItem(strContent,
				"(\\[upload=(.[^\\[]*)\\])(.*?)(\\[\\/upload\\])",
				"<br><IMG SRC=" + imagePath
						+ "$2.gif border=0> <a href=$3>点击浏览该文件</a>", pattern,
				matcher, true);

		// 转换URL
		strContent = ubbItem(strContent, "(\\[url\\])(.*?)(\\[\\/url\\])",
				"<A HREF=$2 TARGET=_blank>$2</A>", pattern, matcher, true);
		strContent = ubbItem(strContent,
				"(\\[url=(.[^\\[]*)\\])(.*?)(\\[\\/url\\])",
				"<A HREF=$2 TARGET=_blank>$3</A>", pattern, matcher, true);

		// EMAIL
		strContent = ubbItem(strContent, "(\\[email\\])(.*?)(\\[\\/email\\])",
				"<img align=absmiddle src=" + imagePath
						+ "email1.gif><A HREF=\"mailto:$2\">$2</A>", pattern,
				matcher, true);
		strContent = ubbItem(
				strContent,
				"(\\[email=(.[^\\[]*)\\])(.*?)(\\[\\/email\\])",
				"<img align=absmiddle src="
						+ imagePath
						+ "email1.gif><A HREF=\"mailto:$2\" TARGET=_blank>$3</A>",
				pattern, matcher, true);

		// 网址
		strContent = ubbItem(strContent,
				"^(http:\\/\\/[A-Za-z0-9\\.\\/=\\?%\\-&_~`@':+!]+)",
				"<img align=absmiddle src=" + imagePath
						+ "url.gif><a target=_blank href=$1>$1</a>", pattern,
				matcher, true);
		strContent = ubbItem(strContent,
				"(http:\\/\\/[A-Za-z0-9\\.\\/=\\?%\\-&_~`@':+!]+)$",
				"<img align=absmiddle src=" + imagePath
						+ "url.gif><a target=_blank href=$1>$1</a>", pattern,
				matcher, true);
		strContent = ubbItem(strContent,
				"[^>=\"\"](http:\\/\\/[A-Za-z0-9\\.\\/=\\?%\\-&_~`@':+!]+)",
				"<img align=absmiddle src=" + imagePath
						+ "url.gif><a target=_blank href=$1>$1</a>", pattern,
				matcher, true);
		strContent = ubbItem(strContent,
				"^(ftp:\\/\\/[A-Za-z0-9\\.\\/=\\?%\\-&_~`@':+!]+)",
				"<img align=absmiddle src=" + imagePath
						+ "url.gif><a target=_blank href=$1>$1</a>", pattern,
				matcher, true);
		strContent = ubbItem(strContent,
				"(ftp:\\/\\/[A-Za-z0-9\\.\\/=\\?%\\-&_~`@':+!]+)$",
				"<img align=absmiddle src=" + imagePath
						+ "url.gif><a target=_blank href=$1>$1</a>", pattern,
				matcher, true);
		strContent = ubbItem(strContent,
				"[^>=\"\"](ftp:\\/\\/[A-Za-z0-9\\.\\/=\\?%\\-&_~`@':+!]+)",
				"<img align=absmiddle src=" + imagePath
						+ "url.gif><a target=_blank href=$1>$1</a>", pattern,
				matcher, true);
		strContent = ubbItem(strContent,
				"^(rtsp:\\/\\/[A-Za-z0-9\\.\\/=\\?%\\-&_~`@':+!]+)",
				"<img align=absmiddle src=" + imagePath
						+ "url.gif><a target=_blank href=$1>$1</a>", pattern,
				matcher, true);
		strContent = ubbItem(strContent,
				"(rtsp:\\/\\/[A-Za-z0-9\\.\\/=\\?%\\-&_~`@':+!]+)$",
				"<img align=absmiddle src=" + imagePath
						+ "url.gif><a target=_blank href=$1>$1</a>", pattern,
				matcher, true);
		strContent = ubbItem(strContent,
				"[^>=\"\"](rtsp:\\/\\/[A-Za-z0-9\\.\\/=\\?%\\-&_~`@':+!]+)",
				"<img align=absmiddle src=" + imagePath
						+ "url.gif><a target=_blank href=$1>$1</a>", pattern,
				matcher, true);
		strContent = ubbItem(strContent,
				"^(mms:\\/\\/[A-Za-z0-9\\.\\/=\\?%\\-&_~`@':+!]+)",
				"<img align=absmiddle src=" + imagePath
						+ "url.gif><a target=_blank href=$1>$1</a>", pattern,
				matcher, true);
		strContent = ubbItem(strContent,
				"(mms:\\/\\/[A-Za-z0-9\\.\\/=\\?%\\-&_~`@':+!]+)$",
				"<img align=absmiddle src=" + imagePath
						+ "url.gif><a target=_blank href=$1>$1</a>", pattern,
				matcher, true);
		strContent = ubbItem(strContent,
				"[^>=\"\"](mms:\\/\\/[A-Za-z0-9\\.\\/=\\?%\\-&_~`@':+!]+)",
				"<img align=absmiddle src=" + imagePath
						+ "url.gif><a target=_blank href=$1>$1</a>", pattern,
				matcher, true);

		// 笑脸转换
		if (strIcons.equals("1")) {
			strContent = ubbItem(strContent, "(\\[em(.*?)\\])", "<img src="
					+ imgName + "$2.gif border=0 align=middle>", pattern,
					matcher, true);
		}

		// COLOR
		strContent = ubbItem(strContent,
				"(\\[color=(.[^\\[]*)\\])(.*?)(\\[\\/color\\])",
				"<font color=$2>$3</font>", pattern, matcher, true);

		// FACE
		strContent = ubbItem(strContent,
				"(\\[face=(.[^\\[]*)\\])(.*?)(\\[\\/face\\])",
				"<font face=$2>$3</font>", pattern, matcher, true);

		// ALIGN
		strContent = ubbItem(strContent,
				"(\\[align=(.[^\\[]*)\\])(.*?)(\\[\\/align\\])",
				"<div align=$2>$3</div>", pattern, matcher, true);

		// 转换FLY
		strContent = ubbItem(strContent, "(\\[fly\\])(.*?)(\\[\\/fly\\])",
				"<marquee width=90% behavior=alternate "
						+ "scrollamount=3>$2</marquee>", pattern, matcher, true);

		// 转换MOVE
		strContent = ubbItem(strContent, "(\\[move\\])(.*?)(\\[\\/move\\])",
				"<MARQUEE scrollamount=3>$2</marquee>", pattern, matcher, true);

		// 转换GLOW
		strContent = ubbItem(
				strContent,
				"\\[glow=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)\\](.*?)\\[\\/glow]",
				"<table width=$1 style=\"filter:"
						+ "glow(color=$2, strength=$3)\">$4</table>", pattern,
				matcher, true);

		// 转换SHADOW
		strContent = ubbItem(
				strContent,
				"\\[shadow=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)\\](.*?)\\[\\/shadow]",
				"<table width=$1 style=\"filter:"
						+ "shadow(color=$2, strength=$3)\">$4</table>",
				pattern, matcher, true);

		// 字体格式
		strContent = ubbItem(strContent, "(\\[i\\])(.*?)(\\[\\/i\\])",
				"<i>$2</i>", pattern, matcher, true);
		strContent = ubbItem(strContent, "(\\[u\\])(.*?)(\\[\\/u\\])",
				"<u>$2</u>", pattern, matcher, true);
		strContent = ubbItem(strContent, "(\\[b\\])(.*?)(\\[\\/b\\])",
				"<b>$2</b>", pattern, matcher, true);
		strContent = ubbItem(strContent,
				"\\[size=([+|-]?[0-7])\\](.*?)(\\[\\/size\\])",
				"<font size=$1>$2</font>", pattern, matcher, true);
		strContent = ubbItem(strContent, "(\\[list\\])(.+?)(\\[\\/list\\])",
				"<UL TYPE=SQUARE>$2</UL>", pattern, matcher, true);
		strContent = ubbItem(strContent,
				"(\\[list=)(A|1)(\\])(.+?)(\\[\\/list\\])",
				"<OL TYPE=$2>$4</OL>", pattern, matcher, true);
		strContent = ubbItem(strContent, "(\\[\\*\\])", "<LI>", pattern,
				matcher, true);
		strContent = ubbItem(strContent,
				"(\\[center\\])(.*?)(\\[\\/center\\])", "<center>$2</center>",
				pattern, matcher, true);
		strContent = ubbItem(
				strContent,
				"(\\[table=(#*[a-z0-9]*),(#*[a-z0-9]*),(#*[a-z0-9]*)\\])(.*?)(\\[\\/table\\])",
				"<table border=0 cellpadding=5 "
						+ "cellspacing=1 bgcolor=$2><tr><td bgcolor=$3><font color=$4>"
						+ "$5</font></td></tr></table>", pattern, matcher, true);

		// QUOTE
		strContent = quote(strContent, pattern, matcher, true);

		return strContent;
	}

	/**
	 * 替换一些特殊符号为html语法标记.
	 * 
	 * @param fString转换前的代码
	 *            .
	 * @return fString转换后的内容.
	 */
	public static String dvHTMLEncode(String fString) {
		if (fString != null && !fString.equals("")) {
			fString = fString.replaceAll("&", "&amp;");
			fString = fString.replaceAll("\t", "&nbsp;&nbsp;&nbsp;");// Tab
			fString = fString.replaceAll("\"", "&quot;");// "
			fString = fString.replaceAll("'", "&#39;");// '
			fString = fString.replaceAll("\r", "");// 回车是一个13+10
			fString = fString.replaceAll(" ", "&nbsp;");
			fString = fString.replaceAll(">", "&gt;");
			fString = fString.replaceAll("<", "&lt;");
			fString = fString.replaceAll("\n", "<BR>");// 换行
			return fString;
		} else {
			return "";
		}

	}

	/*
	 * public static String dvTitleHTMLEncode(String fString) { if (fString !=
	 * null && !fString.equals("")) {
	 * 
	 * fString = fString.replaceAll("\t", "&nbsp;&nbsp;&nbsp;");//Tab fString =
	 * fString.replaceAll("\"", "&quot;");//" fString = fString.replaceAll("'",
	 * "&#39;");//' fString = fString.replaceAll("\r", "");//回车是一个13+10 fString
	 * = fString.replaceAll(" ", "&nbsp;"); fString = fString.replaceAll(">",
	 * "&gt;"); fString = fString.replaceAll("<", "&lt;"); fString =
	 * fString.replaceAll("\n", "<BR>");//换行 fString = fString.replaceAll("&",
	 * "&amp;"); return fString; } else { return ""; }
	 * 
	 * }
	 */

	/**
	 * html语法标记转换符号本身.
	 * 
	 * @param fString转换前的代码
	 *            .
	 * @return fString转换后的内容.
	 */
	public static String dvHTMLCode(String fString) {
		if (fString != null && !fString.equals("")) {
			fString = fString.replaceAll("<P>", "\n\n");
			fString = fString.replaceAll("<BR>", "\n");// 换行
			fString = fString.replaceAll("&nbsp;&nbsp;&nbsp;", "\t");// Tab
			fString = fString.replaceAll("&quot;", "\"");// "
			fString = fString.replaceAll("&#39;", "'");// '
			fString = fString.replaceAll("&nbsp;", " ");
			fString = fString.replaceAll("&gt;", ">");
			fString = fString.replaceAll("&lt;", "<");
			fString = fString.replaceAll("&amp;", "&");
			return fString;
		} else {
			return "";
		}
	}

	/**
	 * 去除标题UBB "[b]标题标题[/b]"--转化后---"标题标题"
	 * 
	 * @param str
	 * @return
	 */
	public static String ubbTitle(String str) {
		String temp = str;
		String re;
		Pattern pattern = null;
		Matcher matcher = null;
		// 去除加粗
		re = "\\[b\\](.*?)\\[\\/b\\]";
		pattern = Pattern.compile(re);
		matcher = pattern.matcher(temp);
		temp = matcher.replaceFirst("$1");
		re = "\\[B\\](.*?)\\[\\/B\\]";
		pattern = Pattern.compile(re);
		matcher = pattern.matcher(temp);
		temp = matcher.replaceFirst("$1");

		// 去除斜体
		re = "\\[i\\](.*?)\\[\\/i\\]";
		pattern = Pattern.compile(re);
		matcher = pattern.matcher(temp);
		temp = matcher.replaceFirst("$1");
		re = "\\[I\\](.*?)\\[\\/I\\]";
		pattern = Pattern.compile(re);
		matcher = pattern.matcher(temp);
		temp = matcher.replaceFirst("$1");

		// 去除颜色
		re = "(\\[color=(.[^\\[]*)\\])(.*?)(\\[\\/color\\])";
		pattern = Pattern.compile(re);
		matcher = pattern.matcher(temp);
		temp = matcher.replaceFirst("$3");
		re = "(\\[COLOR=(.[^\\[]*)\\])(.*?)(\\[\\/COLOR\\])";
		pattern = Pattern.compile(re);
		matcher = pattern.matcher(temp);
		temp = matcher.replaceFirst("$3");
		return temp;
	}

	public static void main(String[] s) {

		String str = "[size=3]aa[/size]";
		System.out.println(Ubbcode.ubbCode(str));
	}
}